package threading;

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

public class ThreadDdemo8 {
    public static void main(String[] args) throws InterruptedException {

        //  实现阻塞队列
        BlockingDeque<String> queue = new LinkedBlockingDeque<>();


        Thread thread1 = new Thread( () -> {
            //  put 入队列
            try {
                queue.put("hello1");
                queue.put("hello2");
                queue.put("hello3");
                queue.put("hello4");

                Thread.sleep(2000);
                queue.put("hello5");
                queue.put("hello6");
                queue.put("hello7");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        } );


        Thread thread2 = new Thread( () -> {
            while (true){
                // take 入队列
                String result = null;
                for (int i = 0; i < 40; i++) {
                    try {
                        result = queue.take();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println(result);
                }
                try {
                    Thread.sleep(7000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        } );


        thread1.start();

        thread2.start();


    }
}
